


var ajax = require("../ajax/ajax");
var mongo = require("../mongo/mongo");

var validator = require("validator");
var moment = require("moment");
var py = require("../comm/py");
const fTask = require('./fTask');
const dTask = require('../dict/task');
const yz = require('../comm/yz');
const mongoose = require('mongoose');
const rong = require('../comm/rong');


function get(req, res, r, c) {
    try {
        var para = req.body.para;


        switch (para) {
            case "saveTask":  //保存用户信息

                saveTask(req, res).then(r).catch(c);
                break;
            case "getTaskList":
                getTaskList(req, res).then(r).catch(c)
                break;
            case "getMyTaskList":
                getMyTaskList(req, res).then(r).catch(c)
                break;
            case "getTask":
                getTask(req, res).then(r).catch(c)
                break;
            case "acceptTask":   //接受任务
                acceptTask(req, res).then(r).catch(c)
                break

            case "doneTask":  //完成任务
                doneTask(req, res).then(r).catch(c)
                break

            case "saveMyTask": //保存任务
                saveMyTask(req, res).then(r).catch(c)
                break

            case "changeMemberMsg": //保存任务
                changeMemberMsg(req, res).then(r).catch(c)
                break


            default:
        }
    } catch (e) {


        console.log(e);
        ajax.re(res, {

            re: "notok",
            err: e,
            errMsg: e
        })
    }


}

async function changeMemberMsg(req) {

    var j = req.body.j;




    var re = await fTask.changeMemberMsg(j)
    return
}


async function saveMyTask(req, res) {
    var j = req.body.j;

    if (!j._id) {
        throw '不能没有id'
    }

    var task = await mongo.findOneAsync({
        data: { _id: j._id },
        collection: "task"

    })
    if (j.defaultSizeId) {
        task.defaultSizeId = j.defaultSizeId
    }
    task.detail = j.detail;
    if (task.detail2) {
        task.detail = j.detail2;   //工价的话有第二个列表

    }


    task = dTask.fmtTask(task)

    var re = await fTask.saveTask(task)

    return




}



async function doneTask(req, res) {
    var j = req.body.j;

    if (!j._id) {
        throw '不能没有id'
    }

    var re = await fTask.doneTask(j._id)
    return


}

async function acceptTask(req, res) {
    var j = req.body.j;

    if (!j._id) {
        throw '不能没有id'
    }


    if (!j.expEndTime) {
        throw "不能没有预期结束时间!"
    }

    var re = await fTask.acceptTask(j._id, j.expEndTime)
    return

}




async function getTask(req, res) {
    var j = req.body.j;

    if (!j._id) {
        throw '不能没有id'
    }

    var info = await mongo.findOneAsync({
        data: {
            _id: j._id

        },
        db: "uzor",
        collection: "task"


    })
    return { info: info }
}


//查询用户列表
async function getMyTaskList(req, res) {

    var j = req.body.j


    var memberId = req.session.member._id
    j.data["$or"] = [{ "managers._id": memberId }, { "members._id": memberId }]

    var data = await mongo.findAsync({

        data: j.data,
        limit: 10,
        db: "uzor",
        collection: "task",
        sort: { statusId: 1, createTime: -1 },
        page: j.page


    })
    if (j.needCustomerOrder) {
        //如果列表中需要订单数据

        var coIdList = [];
        data.list.forEach(function (task) {
            coIdList.push(mongoose.Types.ObjectId(task.customerOrderId))
        }, this);
        var coList = await mongo.findAsync({

            data: {
                _id: { $in: coIdList }
            },
            projection: {
                name: 1,
                imgUrl: 1,
                code: 1,
                ["proof.modelNo"]: 1,
                ["bulk.modelNo"]: 1,
                ["flow"]: 1,

            },
            collection: "customerOrder"

        })



        data.list.forEach(function (task) {
            task.nodeName = "-";
            coList.list.forEach(function (co) {
                if (co._id.toString() == task.customerOrderId) {
                    task.customerOrder = co


                    co.flow[task.flowClassId].nodeList.forEach(function (node) {

                        if (node.id == task.nodeId) {

                            task.nodeName = node.name
                        }


                    }, this);




                }
            }, this);

        }, this);
    }
    return data


}

//查询用户列表
async function getTaskList(req, res) {

    var j = req.body.j

    var data = await mongo.findAsync({

        data: j.data,
        limit: 10,
        db: "uzor",
        collection: "task",
        sort: { createTime: -1 },
        page: j.page


    })

    return data


}

//保存任务,一般来说是修改
async function saveTask(req, res) {

    var j = req.body.j;

    if (!validator.isLength(j.name, { min: 1, max: 100 })) {

        throw "名称长度必须在1到100之间"
    }

    if (!j._id || !j.createTime) {
        j.createTime = moment().utcOffset(-8)._d;
    }
    try {
        j.createTime = moment(j.createTime)._d;
    } catch (e) {

    } finally {

    }

    j.namePy = py.firstLetter(j.name);
    await mongo.SaveAsync({
        data: j,
        collection: "task"
    })

    var re = await fTask.saveTask(j)   //格式化detail

    return
}


module.exports = get;